Button press is not considered an event. An event is binding a function to a mouse, or keyboard action. In this example I get the cursor position upon left click, relative to the top left corner of the screen. The event is bound to the root widget.
import tkinter as tk
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
frame = tk.Frame(root)
frame.pack(fill='x')
self.button = tk.Button(frame, text='M', font='Verdana 13 bold', command= lambda: self.toplevel(x, y))
self.button.pack(side='right', anchor='e', padx=10, pady=10)
def toplevel(self, x, y):
self.toplevel = tk.Toplevel()
self.toplevel.overrideredirect(True)
self.toplevel.geometry(f"100x130+{x}+{y}")
button = tk.Button(self.toplevel, text="Quit")
button.pack(padx=5, pady=5, anchor='w')
def get_event(event):
global x, y
x = event.x_root
y = event.y_root
if __name__ == "__main__":
root = tk.Tk()
root.title('Title')
MainApplication(root).pack(side="top", fill="both", expand=True)
root.bind('', get_event)
root.mainloop()
|